package com.buka.common;

import com.alibaba.fastjson.JSON;

public class Result <T> {

    private int code;//状态码  当前业务逻辑执行成功还是失败

    private String msg;//  如果失败了，错误信息

    private T data;  // 业务逻辑顺利执行，方法的结果 返回的数据

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }


    // 封装一个执行成功返回Result对象，data为返回的数据
    public static <T> String success(T data) {
        Result<T> result = new Result<>();
        result.setCode(200); //业务状态码
        result.setData(data);
        return JSON.toJSONString(result);
    }

    //封装一个error 请求异常返回
    public static <T> String error(String msg) {
        Result<T> result = new Result<>();
        result.setCode(500); //业务失败
        result.setMsg(msg);
        return JSON.toJSONString(result);
    }

}
